package com.ruoyi.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.account.service.IAccountMerchantInfoHisService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.db.domain.AccountMerchantInfoHis;
import com.ruoyi.db.mapper.AccountMerchantInfoHisMapper;
import com.ruoyi.db.mapper.MerchantBaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 商户账户流水Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AccountMerchantInfoHisServiceImpl implements IAccountMerchantInfoHisService {
    @Autowired
    private AccountMerchantInfoHisMapper accountMerchantInfoHisMapper;
    @Resource
    MerchantBaseInfoMapper merchantBaseInfoMapper;

    /**
     * 查询商户账户流水
     *
     * @param id 商户账户流水主键
     * @return 商户账户流水
     */
    @Override
    public AccountMerchantInfoHis selectAccountMerchantInfoHisById(Long id) {
        return accountMerchantInfoHisMapper.selectAccountMerchantInfoHisById(id);
    }

    @Override
    public List<Map<String, Object>> selectAccountOwnerMapList(AccountMerchantInfoHis accountMerchantInfoHis) {
        List<Map<String, Object>> apiList = accountMerchantInfoHisMapper.selectAccountOwnerMapList(accountMerchantInfoHis);
        //List<Map<String, Object>> apiList1 = accountMerchantInfoHisMapper.selectAccountOwnerMapList1(accountMerchantInfoHis);
        List<Map<String, Object>> crmList = accountMerchantInfoHisMapper.selectAccountOwnerCrmMapList(accountMerchantInfoHis);

        Map<String, Map<String, Object>> combinedMap = new HashMap<>();

        mergeRecords(apiList, combinedMap);
        mergeRecords(crmList, combinedMap);
        //mergeRecords(apiList1, combinedMap);
        List<Map<String, Object>> resultList = new ArrayList<>(combinedMap.values());


        resultList.sort((record1, record2) -> {
            BigDecimal totalChangeAmount1 = (BigDecimal) record1.get("totalChangeAmount");
            BigDecimal totalChangeAmount2 = (BigDecimal) record2.get("totalChangeAmount");
            return totalChangeAmount2.compareTo(totalChangeAmount1);
        });

        return resultList;
    }

    private void mergeRecords(List<Map<String, Object>> sourceList, Map<String, Map<String, Object>> combinedMap) {
        for (Map<String, Object> record : sourceList) {
            String ownerName = (String) record.get("ownerName");
            if (ownerName == null) {
                continue;
            }

            if (combinedMap.containsKey(ownerName)) {
                Map<String, Object> combinedRecord = combinedMap.get(ownerName);
                combinedRecord.put("totalChangeAmount", ((BigDecimal) combinedRecord.get("totalChangeAmount")).add((BigDecimal) record.get("totalChangeAmount")));
            } else {
                combinedMap.put(ownerName, new HashMap<>(record));
            }
        }
    }


    @Override
    public List<Map<String, Object>> selectAccountHisMapList(AccountMerchantInfoHis accountMerchantInfoHis) {
        List<Map<String, Object>> ApiList = accountMerchantInfoHisMapper.selectAccountHisMapList(accountMerchantInfoHis);
        List<Map<String, Object>> CrmList = accountMerchantInfoHisMapper.selectAccountHisCrmMapList(accountMerchantInfoHis);

        List<Map<String, Object>> List = new ArrayList<>();
        List.addAll(ApiList);
        List.addAll(CrmList);
        return List;
    }


    @Override
    public List<Map<String, Object>> selectAccountHisMapList1(AccountMerchantInfoHis accountMerchantInfoHis) {

        List<Map<String, Object>> apiList = accountMerchantInfoHisMapper.selectAccountHisMapList1(accountMerchantInfoHis);
        List<Map<String, Object>> crmList = accountMerchantInfoHisMapper.selectAccountHisCrmMapList1(accountMerchantInfoHis);


        List<Map<String, Object>> combinedList = new ArrayList<>();
        combinedList.addAll(apiList);
        combinedList.addAll(crmList);


        combinedList.sort((map1, map2) -> {
            BigDecimal totalChangeAmount1 = (BigDecimal) map1.get("totalChangeAmount");
            BigDecimal totalChangeAmount2 = (BigDecimal) map2.get("totalChangeAmount");
            return totalChangeAmount2.compareTo(totalChangeAmount1);
        });

        return combinedList;
    }


    /**
     * 查询商户账户流水列表
     *
     * @param accountMerchantInfoHis 商户账户流水
     * @return 商户账户流水
     */
    @Override
    public List<AccountMerchantInfoHis> selectAccountMerchantInfoHisList(AccountMerchantInfoHis accountMerchantInfoHis) {
        return accountMerchantInfoHisMapper.selectAccountMerchantInfoHisList(accountMerchantInfoHis);
    }

    /**
     * 查询商户账户流水列表
     *
     * @param accountMerchantInfoHis 商户账户流水
     * @return 商户账户流水
     */
    @Override
    public List<Map<String, Object>> selectAccountMerchantInfoHisMapList(AccountMerchantInfoHis accountMerchantInfoHis) {

        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            Long deptId = SecurityUtils.getDeptId();
            Long InfoId = merchantBaseInfoMapper.seletInfoIdBydeptId(deptId);
            accountMerchantInfoHis.setAccountId(InfoId);
            startPage();
        }

        return accountMerchantInfoHisMapper.selectAccountMerchantInfoHisMapList(accountMerchantInfoHis);
    }
    @Override
    public List<Map<String, Object>> selectAccountHisList(AccountMerchantInfoHis accountMerchantInfoHis) {

        return   accountMerchantInfoHisMapper.selectAccountHisList(accountMerchantInfoHis);
    }
    @Override
    public String selectAccountHisAllList(AccountMerchantInfoHis accountMerchantInfoHis) {

        return  accountMerchantInfoHisMapper.selectAccountHisAllList(accountMerchantInfoHis);
    }

    @Override
    public List<Map<String, Object>> selectAccountMerchantInfoHisMapList1(AccountMerchantInfoHis accountMerchantInfoHis) {
        return accountMerchantInfoHisMapper.selectAccountMerchantInfoHisMapList(accountMerchantInfoHis);
    }

    /**
     * 新增商户账户流水
     *
     * @param accountMerchantInfoHis 商户账户流水
     * @return 结果
     */
    @Override
    public int insertAccountMerchantInfoHis(AccountMerchantInfoHis accountMerchantInfoHis) {
        accountMerchantInfoHis.setCreateTime(DateUtils.getNowDate());
        return accountMerchantInfoHisMapper.insertAccountMerchantInfoHis(accountMerchantInfoHis);
    }

    /**
     * 修改商户账户流水
     *
     * @param accountMerchantInfoHis 商户账户流水
     * @return 结果
     */
    @Override
    public int updateAccountMerchantInfoHis(AccountMerchantInfoHis accountMerchantInfoHis) {
        accountMerchantInfoHis.setUpdateTime(DateUtils.getNowDate());
        return accountMerchantInfoHisMapper.updateAccountMerchantInfoHis(accountMerchantInfoHis);
    }

    /**
     * 批量删除商户账户流水
     *
     * @param ids 需要删除的商户账户流水主键
     * @return 结果
     */
    @Override
    public int deleteAccountMerchantInfoHisByIds(Long[] ids) {
        return accountMerchantInfoHisMapper.deleteAccountMerchantInfoHisByIds(ids);
    }

    /**
     * 删除商户账户流水信息
     *
     * @param id 商户账户流水主键
     * @return 结果
     */
    @Override
    public int deleteAccountMerchantInfoHisById(Long id) {
        return accountMerchantInfoHisMapper.deleteAccountMerchantInfoHisById(id);
    }

    @Override
    public List<Map<String, Object>> getDataSortOrganizeApi(Integer type) {

        LocalDate dateInfo = LocalDate.now();

        if (type == 0 || type == 1) {
            dateInfo = (type == 0) ? dateInfo : dateInfo.minusDays(1); // 今天或者昨天的日期
        } else if (type == 2 || type == 3) {
            int date = (type == 2) ? 3 : 5; // 根据类型确定要减去的天数
            LocalDate startDate = dateInfo.minusDays(date); // 开始日期
            LocalDate endDate = LocalDate.now().minusDays(1); // 结束日期

            BigDecimal number = accountMerchantInfoHisMapper.AllNumber(startDate.toString(), endDate.toString());
            List<Map<String, Object>> maplist = accountMerchantInfoHisMapper.getAllDataSortOrganizeApi(startDate.toString(), endDate.toString());

            Map<String, Object> totalMap = new HashMap<>();
            totalMap.put("name", "总消耗");
            totalMap.put("number", number);
            maplist.add(0, totalMap);

            return maplist;
        }

        BigDecimal number = accountMerchantInfoHisMapper.countAuthNumber(dateInfo.toString());
        List<Map<String, Object>> maplist = accountMerchantInfoHisMapper.getDataSortOrganizeApi(dateInfo.toString());

        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("name", "总消耗");
        totalMap.put("number", number);
        maplist.add(0, totalMap);

        return maplist;
    }

    @Override
    public String selectAccountHisAllList1(AccountMerchantInfoHis accountMerchantInfoHis) {
        return  accountMerchantInfoHisMapper.selectAccountHisAllList1(accountMerchantInfoHis);
    }

    @Override
    public int deleteAccountMerchantHisInfoByDateInfo(String dateInfo) {
        return accountMerchantInfoHisMapper.deleteAccountMerchantHisInfoByDateInfo(dateInfo);
    }


}
